/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../../tools/PacketCreator.h"
#include "../../tools/PacketReader.h"
#include "PlayerHandler.h"
#include "../../server/Player.h"
#include "../../server/Map.h"
#include "../../server/Summon.h"
#include "../../server/Summons.h"
#include "../../server/ObjectMoving.h"
#include "../../server/PlayerBuffs.h"

void PlayerHandler::summonMove(PacketReader& packet){
	Summon* summon = player->getSummons()->getSummonById(packet.readInt());
	packet.read(4);
	if(summon == 0 || summon->getMovType() == 0)
		return;
	Position pos;
	pos.x = summon->getPosition().x;
	pos.y = summon->getPosition().y;
	ObjectMoving moving = ObjectMoving(packet, summon);
	player->getMap()->send(PacketCreator().moveSummon(player->getID(), summon->getObjectID(), pos, moving));
}

void PlayerHandler::damageSummon(PacketReader& packet){
	int summonid = packet.readInt();
	char notsure = packet.read();
	int damage = packet.readInt();
	int mobid = packet.readInt();
	if(Summon* summon = player->getSummons()->getSummonById(summonid)) {
		summon->setHp(summon->getHp() - damage);
		// commented in vana, is this needed?
		player->getMap()->send(PacketCreator().damageSummon(player->getID(), summonid, notsure, damage, mobid));
		if(summon->getHp() <= 0) {
			player->getBuffs()->cancelBuff(summon->getSkillid());
			player->getSummons()->removeSummon(summon->getSkillid());
		}
	}
}